package com.lane.sidecn.product.web;

import org.springframework.beans.BeanUtils;

import com.lane.core.web.BaseController;
import com.lane.sidecn.product.model.Category;

/**
 * @author 张先宙 E-mail: zhangxianzhou_std@163.com
 * @version 1.0 创建时间：Jun 30, 2009 10:40:55 PM
 * 
 * 商品类别管理控制器
 */

public class CategoryController extends BaseController {

	private Category category;

	/**
	 * 添加新的商品类别
	 */
	public void add() {
		if (category == null) {
			printMessage("addFail");// 添加新的类别失败
		}
		try {
			baseManager.save(category);// 调用接口进行保存
			printMessage("addSuccess");// 添加新的类别成功

		} catch (Exception e) {
			printMessage("addFail");// 添加新的类别失败
		}

	}

	/**
	 * 通过类别的名字，删除已有的商品类别
	 */
	public void removeByName() {
		if (category == null || category.getName() == null) {
			printMessage("removeFail");// 对象为空或者是名字为空，删除失败
		}
		try {
			Category cat = baseManager.findUniqueBy(Category.class, "catname",
					category.getName());

			baseManager.remove(cat);// 删除商品的类别
			printMessage("removeSuccess");// 删除商品成功

		} catch (Exception e) {
			printMessage("removeFail");// 删除商品失败
		}

	}

	/**
	 * 通过商品的名字，找到对应的商品对象
	 * 
	 */

	public Category findByName() {
		if (category == null) {
			return null;
		}
		Category cat = baseManager.findUniqueBy(Category.class, "catname",
				category.getName());

		return cat;
	}

	/**
	 * 更新商品类型
	 * 
	 */

	public void update() {
		Category oldCategory = findByName();
		if (oldCategory == null) {
			printMessage("updateFail");//更新失败
			return;
		}
		BeanUtils.copyProperties(oldCategory, category);//拷贝属性
		try {
			baseManager.save(category);//保存更新
			printMessage("updateSuccess");//更新成功
		} catch (Exception e) {
			printMessage("updateFail");//更新失败
		}

	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
}
